#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys


def getPort():
    """
    获取端口 默认8000
    :return: 
    """
    port = 5037
    if len(sys.argv) > 1:
        port = sys.argv[1]
    return port


def cmd():
    """
    列出端口的占用进程
    :return: 
    """
    port = getPort()
    f = os.popen(r"netstat -nao | findstr %s" % (port), "r")
    result = f.read()
    f.close()
    return result


def cmd2(pid):
    """
    结束进程
    :param pid: 
    :return: 
    """
    if pid == None or len(pid) < 1 or pid == "0":
        return
    os.system("taskkill /pid %s /F" % (pid))


if __name__ == '__main__':
    result = cmd()
    result = result.split("\n")
    for item in result:
        item1 = item.split(" ");
        cmd2(item1[len(item1) - 1])
